package cn.szzsi._sys;

import cn.szzsi._Config;
import cn.szzsi._admin.BaseAdminController;
import cn.szzsi.common.kit.CommonUtil;
import cn.szzsi.common.model.Article;
import cn.szzsi.common.model.Member;
import cn.szzsi.common.model.Product;
import cn.szzsi.common.model.SiteConfig;
import cn.szzsi.common.model.zshop.CompanyInfo;
import cn.szzsi.service.HtmlService;
import cn.szzsi.service.SysHtmlService;
import com.jfinal.core.Controller;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;

import java.util.List;

/**
 * 商户模板生成
 *
 */
public class GenerateController extends Controller{
    private String shopId;

    public void page(){
        render("page.html");
    }

    public void index(){
        SysHtmlService.service.indexBuildHtml();
        renderJson(Ret.ok());
        //renderHtml("<script>alert('生成成功');history.go(-1);</script>");
    }

    //一键生成商家店铺
    public void businessIndex(){
        long start=System.currentTimeMillis();
        List<SiteConfig>siteConfigList=SiteConfig.dao.find("select * from mall_site_config");
        for(final SiteConfig siteConfig:siteConfigList){
            _Config.fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    HtmlService.service.indexBuildHtml(siteConfig);
                }
            });

        }
        long end =System.currentTimeMillis();
        System.out.println("-----------------------"+(end-start)/1000);
        renderJson(Ret.ok());
    }


    public void product(){
        long start=System.currentTimeMillis();
        List<Product> list = Product.dao.getAll("createDate","desc");
        for(final Product p:list){
            _Config.fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    SiteConfig siteConfig= SiteConfig.getByShopId(p.getUid());
                    SysHtmlService.service.businessProductContentBuildHtml(p,siteConfig);
                }
            });

        }
        long end =System.currentTimeMillis();
        System.out.println("-----------------------"+(end-start)/1000);
        renderJson(Ret.ok());
    }
    public void article(){
        shopId=getAttr("shopId");
        //获取店铺别名
        SiteConfig siteConfig= SiteConfig.getByShopId(shopId);
        if(siteConfig==null){
            renderHtml("<script>alert('请先在 我要开店 中填写店铺基本信息!');history.go(-1);</script>");
            return;
        }
        String shopAlias=siteConfig.getStr("shopAlias");
        List<Article> list = Article.dao.find("select * from mall_article where uid = ?",shopId);
        boolean isMobileDevice = CommonUtil.isMobileDevice(getRequest());
        String path= CommonUtil.getPathByUidAndType(shopId,isMobileDevice);
        for(Article r:list){
            SysHtmlService.service.businessArticleContentBuildHtml(r,isMobileDevice,path,shopId,shopAlias);
        }
        renderJson(Ret.ok());
    }


}

















